#include<iostream>
#include<algorithm>
using namespace std;

#define lson u<<1
#define rson u<<1|1
#define MAXN 16100

bool vis[MAXN>>1][MAXN>>1];

struct Dat{
	int y1,y2,x;
}seg[MAXN>>1];

struct Node{
	int lef,rig,color;
}T[MAXN<<2];

void Build(int u,int l,int r){
	T[u].lef=l;T[u].rig=r;
	T[u].color=0;
	if(l==r)return;
	int mid=(l+r)>>1;
	Build(lson,l,mid);Build(rson,mid+1,r);
}

void PushDown(int u)
{
	if(T[u].color!=-1)
	{
		T[lson].color=T[rson].color=T[u].color;
		T[u].color=-1;
	}
}

void Update(int u,int l,int r,int val)
{
	if(l<=T[u].lef&&T[u].rig<=r){T[u].color=val;return;}
	else 
	{
		PushDown(u);
		if(l<=T[lson].rig)Update(lson,l,r,val);
		if(r>=T[rson].lef)Update(rson,l,r,val);
	}
}

void Query(int u,int l,int r,int id)
{
	if(T[u].color!=-1)
	{
		vis[T[u].color][id]=true;return;
	}
	if(T[u].lef==T[u].rig)return;
	PushDown(u);
	if(l<=T[lson].rig)Query(lson,l,r,id);
	if(r>=T[rson].lef)Query(rson,l,r,id);
}

int cmp(Dat a,Dat b){return a.x<b.x;}

int main()
{
	int t;
	int n,i,j,k;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(i=1;i<=n;i++)
		{
			scanf("%d%d%d",&seg[i].y1,&seg[i].y2,&seg[i].x);
			seg[i].y1<<=1;seg[i].y2<<=1;
		}
		sort(seg+1,seg+n+1,cmp);
		memset(vis,false,sizeof(vis));
		Build(1,0,MAXN);
		for(i=1;i<=n;i++)
		{
			Query(1,seg[i].y1,seg[i].y2,i);
			Update(1,seg[i].y1,seg[i].y2,i);
		}
		int ans=0;
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=n;j++)
			{
				if(vis[i][j])for(k=1;k<=n;k++)
				{
					if(vis[i][k]&&vis[j][k])ans++;
				}
			}
		}
		printf("%d\n",ans);
	}
}
